package variable.lazy;

/**
 * A lazy value that only initializes when getValue is first called. The method
 * initialize() should never be called directly, it is called internally by
 * getValue. One way to avoid calling initialize() is by declaring the LazyValue
 * variable to be of type Lazy, in which case only the reset() and getValue() 
 * methods are available. It is also recommended that the LazyValue variable
 * be declared final, to prevent accidental resetting to null. Example usage:
 * <br />
 * final Lazy&lt;String&gt; LAZY_STRING = new LazyValue&lt;String&gt;() { <br />
 * public String initialize() { return "abc"; }; <br />
 * };
 * 
 * @author Shimu
 * @date 2-Nov-2012
 */
public abstract class LazyValue<T> implements Lazy<T> {

    private T obj;

    /**
     * Resets the LazyValue
     */
    @Override
    public void reset() {
        this.obj = null;
    }

    /**
     * Returns the LazyValue
     * @return 
     */
    @Override
    public T getValue() {
        if (obj == null) {
            obj = initialize();
        }
        return obj;
    }

    /**
     * Initialize the LazyValue
     * @return 
     */
    public abstract T initialize();
}
